/* Bugzilla C# Proxy Library
   Copyright (C) 2006, Dansk BiblioteksCenter A/S
   Mads Bondo Dydensborg, <mbd@dbc.dk>
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/   
/*! \file
  \brief This is a test program, that simply tests that all methods are callable.

  This is a very simple/naive tests, but it does catch errors in the
  various interfaces and most of the conversions done by
  xml-rpc.net. No check of the validity of results though.
*/


using System;
using System.IO;
using System.Net;
using CookComputing.XmlRpc;

using Bugzproxy;

////////////////////////////////////////////////////////////////////////////////
class TestClass { 
  
  // Argument parser
  static SimpleAppArgs argParser;
  
  // Used for all the tests.
  static private Server bugzilla;

  //////////////////////////////////////////////////////////////////////
  // Get version and timezone - in CVS
  static void TestVersionTimeZone() {
    bugzilla.GetVersion();
    bugzilla.GetTimezone();
    Console.WriteLine( "TestVersionTimezone passed" );
  }
  
  //////////////////////////////////////////////////////////////////////
  // Login/out - in CVS
  static void Login() {
    try {
      bugzilla.Login( argParser.User, argParser.Password, true );
    } catch ( Exception ) {
      Console.WriteLine( "Login failed - trying to proceed anyway" );
      return;
    }
    Console.WriteLine( "Login passed" );
  }

  static void Logout() {
    bugzilla.Logout();
    Console.WriteLine( "Logout passed" );
  }
  
  //////////////////////////////////////////////////////////////////////
  // CreateBug
  static void TestCreateBug() {
    try {
      // Create a bug -- experimental
      bugzilla.GetProduct( 1 )
        .CreateBug( null, "WeatherControl",
                    "1.0", "All", "All",
                    "Test bug from bugzproxy (samples/Test)", 
                    "Created by bugzproxy - a C# library for the Bugzilla WebService API - see http://oss.dbc.dk/bugzproxy.",
                    "P2", "normal", null,
                    null, null, null,
                    null );
    }
    catch ( Exception e ) {
      throw new Exception( "TestCreateBug failed:\n" + e.Message + "\n" + e );
    }
    Console.WriteLine( "TestCreateBug passed" );
  }

  //////////////////////////////////////////////////////////////////////
  // List products - in CVS.
  static void TestListProducts() {
    try {
      bugzilla.GetSelectableProductIds();
      bugzilla.GetEnterableProductIds();
      bugzilla.GetAccessibleProductIds();
    }
    catch ( Exception e ) {
      throw new Exception( "TestListProducts failed:\n" + e.Message + "\n" + e );
    }
    Console.WriteLine( "TestListProducts passed" );
  }

  //////////////////////////////////////////////////////////////////////
  // Get products
  static void TestGetProducts() {
    try {
      int[] ids = new int[] { 1, 3, 4, 5 };
      bugzilla.GetProducts( ids );     
      bugzilla.GetProduct( 1 );
    }
    catch ( Exception e ) {
      throw new Exception( "TestGetProducts failed:\n" + e.Message + "\n" + e );
    }
    Console.WriteLine( "TestGetProducts passed" );
  }
  
  //////////////////////////////////////////////////////////////////////
  // Get a bug 
  static void TestGetBugs() {
    try {
      // List the id and summary of each bug
      int[] ids = new int[] { 1, 2, 3 };
      bugzilla.GetBugs( ids );
      bugzilla.GetBug( 1 );
    }
    catch ( Exception e ) {
      throw new Exception( "TestGetBugs failed:\n" + e.Message + "\n" + e );
    }
    Console.WriteLine( "TestGetBugs passed" );
  }
  
  //////////////////////////////////////////////////////////////////////
  // Get a list of components for product 0
  static void TestGetComponents() {
    try {
      bugzilla.GetProduct( 1 ).GetComponents();
    } 
    catch ( Exception e ) {
      throw new Exception( "TestGetComponents failed:\n" + e.Message + "\n" + e );
    };
    Console.WriteLine( "TestGetComponents passed" );
  }


  //////////////////////////////////////////////////////////////////////
  // Append a comment/set resolution, patch in bug 355847
  static void TestAppendComment( ) {
    try {
      bugzilla.GetBug( 1 ).AppendComment( "This is a test comment with\nmultiple lines, and non-ascii/latin1 chars: æøå ÆØÅ", false, null );
    }
    catch ( Exception e ) {
      throw new Exception( "TestAppendComment failed:\n" + e.Message + "\n" + e );
    };
    Console.WriteLine( "TestAppendComment passed" );
  }

  static void TestWriteCookies( string filename ) {
    FileStream s = File.Create( filename );
    bugzilla.WriteCookies( s );
    s.Close();
    Console.WriteLine( "TestWriteCookies passed" );
  }

  static void TestReadCookies( string filename ) {
    FileStream s = File.OpenRead( filename );
    bugzilla.ReadCookies( s );
    s.Close();
    Console.WriteLine( "TestReadCookies passed" );
  }

  
  //////////////////////////////////////////////////////////////////////
  // MAIN
  
  static public int Main( string[] args ) { 
    argParser 
      = new SimpleAppArgs( "Test.exe", 
                           "Program to test various calls to the Bugzilla WebService api" );
    // If parsing fails, bail out
    if ( ! argParser.Parse( args ) ) { 
      return 1; 
    }
    Console.WriteLine( "Using server {0}:{1}/{2}", argParser.Host, argParser.Port, argParser.Path );
    Console.WriteLine( "Using username {0} and password {1}", argParser.User, argParser.Password );



    // Set up the bugzilla server/proxy/stubb
    bugzilla = new Server( argParser.Host, argParser.Port, argParser.Path );
    if ( argParser.Trace ) { 
      bugzilla.TraceWriter = System.Console.Out; 
    }
    
    //////////////////////////////////////////////////////////////////////
    // Actual tests.
    
    TestVersionTimeZone();
    Login();
    // TestReadCookies( "test2.cookies" );
    // These should be pretty OK
    TestListProducts();
    TestGetProducts();
    TestGetComponents();

    TestGetBugs();
    TestCreateBug();
    // TestAppendComment(); - this seems to not currently (jan 2008) be supported by landfill
    
    TestWriteCookies( "test2.cookies" );
    Logout();

    Console.WriteLine( "All tests done" );
    return 0;
  } 
  
}

